package com.asia5b.a5mall.member.fragment

import android.app.Activity
import android.content.Intent
import android.view.View
import android.view.ViewGroup
import com.asia5b.a5mall.member.R
import com.asia5b.a5mall.member.activity.accout.ThirdProfileActivity
import com.asia5b.a5mall.member.databinding.FragmentThirdprofileBinding
import com.asia5b.a5mall.middleware.base.SimpleFragment
import com.asia5b.a5mall.middleware.base.SimpleViewModel
import com.asia5b.a5mall.middleware.config.UserRepository
import com.asia5b.a5mall.middleware.utils.WImageLoader
import com.asia5b.a5mall.middleware.utils.showMessage
import com.asia5b.a5mall.middleware.utils.then
import kotlinx.android.synthetic.main.fragment_thirdprofile.*

/**
 * Created by zhangyuncai on 2020/5/18.
 */
class ThirdProfileFragment : SimpleFragment<SimpleViewModel, FragmentThirdprofileBinding>() {
    override  val viewModelClazz: Class<SimpleViewModel>
        get() = SimpleViewModel::class.java



    override fun getDataBinding(container: ViewGroup?): FragmentThirdprofileBinding {
        thirdProfileActivity = mActivity as ThirdProfileActivity
        return FragmentThirdprofileBinding.inflate(layoutInflater,container,false)
    }
    lateinit var thirdProfileActivity: ThirdProfileActivity

    override fun initView() {
        setUserInfoView()

    }

    /**
     * 设置用户信息
     */
    fun setUserInfoView() {
        if (UserRepository.isLogin()) {
            ll_thirdprofile_gen.visibility = View.VISIBLE
            val info = UserRepository.getUserInfo()
            info.then { model ->
                WImageLoader.displayRoundImage(model.face, iv_thirdprofile_head, 5)
                iv_thirdprofile_username.text = model.nikename
            }
        } else {

        }
    }

    override fun initEvent() {
        bt_thirdprofile_agree.setOnClickListener {
            val intent = Intent()
            val map = hashMapOf<String, Any>()
            val userInfo = UserRepository.getUserInfo()
            map.put("username", userInfo.username)
            map.put("nikename", userInfo.nikename)
            map.put("uid", userInfo.open_id)
            map.put("sex", userInfo.sex)
            map.put("face", userInfo.face)
            intent.putExtra("profile", map)
            thirdProfileActivity.setResult(Activity.RESULT_OK, intent)
            thirdProfileActivity.finish()
        }
        bt_thirdprofile_reject.setOnClickListener {
            showMessage(R.string.授权失败)
            thirdProfileActivity.finish()
        }
    }

    override fun lazyFetchData() {
        setUserInfoView()
    }

}